package jee.boot.common.basic.interceptor.impl;

import jee.boot.common.basic.DataEntity;
import jee.boot.common.basic.interceptor.PreInsertInterceptor;
import jee.boot.entity.auth.Account;
import jee.boot.common.standard.AccountHandler;
import jee.boot.common.utils.SpringContextHolder;
import jee.boot.common.utils.StringUtils;

import java.util.Date;
import java.util.UUID;

public class DefaultPreInsertInterceptor implements PreInsertInterceptor {
    /**
     * 执行前置操作
     *
     * @param entity
     */
    @Override
    public void execute(DataEntity<?> entity) {
        entity. setId(UUID.randomUUID().toString().replaceAll("-", ""));
        Account account = SpringContextHolder.getApplicationContext().getBean(AccountHandler.class).getPrincipal();
        if(account!=null){
            entity.setUpdateBy(account.shortInfo());
            entity.setCreateBy(account.shortInfo());
        }
        entity.setCreateDate(new Date());
        entity.setUpdateDate(entity.getCreateDate());
        if(StringUtils.isBlank(entity.getStatus())){
            entity.setStatus(entity.DATA_STATUS_ACTIVE);
        }
    }
}
